package dataStructures;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import org.apache.hadoop.io.Writable;

public class UsersMapOutputWritable implements Writable{
	
	private int[] userIds;
	private Ratings[] cbElements;

	public UsersMapOutputWritable() {
	}
	
	public int[] getUserIds(){
		return userIds;
	}
	
	public ArrayList<Integer> getUserIdsInIntegers(){
		ArrayList<Integer> ans = new ArrayList<Integer>();
		for(int i = 0 ; i < userIds.length ; i++){
			ans.add(new Integer(userIds[i]));
		}
		return ans;
		
	}
	
	public Ratings[] getCbElements(){
		return cbElements;
	}
	
		
	public UsersMapOutputWritable(int[] userIds, Ratings[] cbElements) {
		super();
		this.userIds = userIds;
		this.cbElements = cbElements;
	}
	
	@Override
	public void readFields(DataInput in) throws IOException {
		int numOfUserIds = in.readInt();
		this.userIds = new int[numOfUserIds];
		for (int i = 0; i < numOfUserIds; i++) {
			userIds[i] = in.readInt();
		}
		int numOfRatings = in.readInt();
		this.cbElements = new Ratings[numOfRatings];
		for (int i = 0; i < numOfRatings; i++) {
			int ratingsCounter = in.readInt();
			int ratingsSum = in.readInt();
			cbElements[i] = new Ratings(ratingsCounter, ratingsSum);
		}
		
	}

	@Override
	public void write(DataOutput out) throws IOException {
		out.writeInt(this.userIds.length);
		for (int userId : this.userIds) {
			out.writeInt(userId);
		}
		
		out.writeInt(this.cbElements.length);
		for (Ratings ratings : this.cbElements) {
			out.writeInt(ratings.getRatingsCounter());
			out.writeInt(ratings.getRatingsSum());
		}
	}
	
	@Override
	public String toString() {
		StringBuilder out = new StringBuilder();
		for (int i=0; i<this.cbElements.length; i++){
			
		}
		out.
		 append(Arrays.toString(this.userIds)).
		 append("	").
		 append(Arrays.toString(this.cbElements));

		return out.toString();
	}
	
	public static UsersMapOutputWritable fromString(String line) {
		UsersMapOutputWritable usersOutput = null;
		
		int[] userIds;
		Ratings[] cbElements;
		
		String[] lineArray = line.split("	");
		
		String[] userIdsStringArray = lineArray[0].substring(1, lineArray[0].length() - 1).split(",");
		userIds = new int[userIdsStringArray.length];
		for (int i = 0; i < userIdsStringArray.length; i++) {
			userIds[i] = Integer.parseInt(userIdsStringArray[i].trim());
		}
		
		String[] cbElementsStringArray = lineArray[1].substring(1, lineArray[1].length() - 1).split(",");
		cbElements = new Ratings[cbElementsStringArray.length];
		for (int i = 0; i < cbElementsStringArray.length; i++) {
			String sentenence = cbElementsStringArray[i].trim();
			String[] ratings = sentenence.substring(1, sentenence.length() - 1).split(",");
			int ratingsCounter = 0;
			int ratingsSum = 0;
			for (int j = 0; j < ratings.length; j+=2) {
				ratingsCounter = Integer.parseInt(ratings[j].trim());
				ratingsSum = Integer.parseInt(ratings[j+1].trim());
			}
			cbElements[i] = new Ratings(ratingsCounter, ratingsSum);
		}
		
		usersOutput = new UsersMapOutputWritable(userIds, cbElements);
		
		return usersOutput;
	}

}
